﻿//using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

using MusicPlayer.InfraStructure.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Win32;
using MusicPlayer.Core.Utils;
using MusicPlayer.InfraStructure.Model;


using MusicPlayer.Core.Foundation;
using GalaSoft.MvvmLight.Messaging;
using Newtonsoft.Json;
using System.IO;
using Caliburn.MusicPlayer.UserControls;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Windows;
using static MusicPlayer.InfraStructure.Model.Enums;

namespace Caliburn.MusicPlayer.ViewModels
{
    public class MainViewModel: ViewModelBase
    {

        IPlayer player;

        private PlayStatus playerStatus;

        public PlayStatus PlayerStatus
        {
            get { return playerStatus; }
            set
            {
                playerStatus = value;
                RaisePropertyChanged(nameof(PlayerStatus));
            }
        }



        private VolumeHelper volumeHelper = new VolumeHelper();

        public Audio selectedAudio { get; set; }

        private double audioProgress;

        public double AudioProgress
        {
            get { return audioProgress; }
            set
            {
                audioProgress = value;
                RaisePropertyChanged(nameof(AudioProgress));
            }
        }

        private bool skinpopupshow = false;

        /// <summary>
        /// 换肤弹出框的展示与否
        /// </summary>
        public bool skinPopupShow
        {
            get
            {
                return skinpopupshow;
            }
            set
            {
                this.skinpopupshow = value;
                RaisePropertyChanged("skinPopupShow");
            }
        }

        private double programVolume = 30;

        public double ProgramsVolume
        {
            get { return programVolume; }
            set
            {
                programVolume = value;
                RaisePropertyChanged(nameof(ProgramsVolume));
            }
        }

        private ObservableCollection<SongList> songLists = new ObservableCollection<SongList>();

        public ObservableCollection<SongList> SongLists
        {
            get { return songLists; }
            set
            {
                songLists = value;
                RaisePropertyChanged(nameof(SongLists));
            }
        }

        private SongList selectedList;

        public SongList SelectedList
        {
            get { return selectedList; }
            set
            {
                selectedList = value;
                RaisePropertyChanged(nameof(SelectedList));
            }
        }



        private RelayCommand<double> volumeChangedCommand;

        public RelayCommand<double> VolumeChangedCommand
        {
            get
            {
                if (volumeChangedCommand == null)
                {
                    volumeChangedCommand = new RelayCommand<double>((p) =>
                    {
                        volumeHelper.SetCurrentVolume(Convert.ToInt32(p));
                    });
                }
                return volumeChangedCommand;
            }

        }

        private RelayCommand start_StopCommand;

        public RelayCommand Start_StopCommand
        {
            get
            {
                if (start_StopCommand == null)
                {
                    start_StopCommand = new RelayCommand(() =>
                    {
                        player.Init();
                        player.Play(selectedAudio.FullPath);
                    }, () => selectedAudio != null);
                }
                return start_StopCommand;
            }

        }

        private RelayCommand playPreviousCommand;

        public RelayCommand PlayPreviousCommand
        {
            get
            {
                if(playPreviousCommand == null)
                {
                    playPreviousCommand = new RelayCommand(()=> 
                    {
                        MessageBox.Show("播放上一首！");
                    });
                }
                return playPreviousCommand;
            }
        }



        private RelayCommand openFileCommand;

        public RelayCommand OpenFileCommand
        {
            get
            {
                if (openFileCommand == null)
                {
                    openFileCommand = new RelayCommand(()=>
                    {
                        AddNewSongList addWindow = new AddNewSongList();
                        addWindow.Show();
                    });
                }
                return openFileCommand;
            }
            
        }

        private RelayCommand playCommand;

        public RelayCommand PlayCommand
        {
            get
            {
                if(playCommand == null)
                {
                    playCommand = new RelayCommand(() =>
                    {
                        if(player.PlayStat == PlayStatus.Pause)
                        {
                            player.Continue();
                        }
                        if(player.PlayStat == PlayStatus.Playing)
                        {
                            player.Stop();
                        }
                        if(player.PlayStat == PlayStatus.Standby || player.PlayStat == PlayStatus.Stop)
                        {
                            player.Init();
                            player.Play(selectedAudio.FullPath);
                        }
                    },  () => { return File.Exists(selectedAudio?.FullPath); });
                }
                return playCommand;
            }
        }

        private RelayCommand playNextCommand;

        public RelayCommand PlayNextCommand
        {
            get
            {
                if (playNextCommand == null)
                {
                    playNextCommand = new RelayCommand(() =>
                    {

                    });
                }
                return playNextCommand;
            }
        }



        private RelayCommand onClosingCommand;

        public RelayCommand OnClosingCommand
        {
            get
            {
                if (onClosingCommand == null)
                {
                    onClosingCommand = new RelayCommand(() =>
                    {
                        Messenger.Default.Unregister<SongList>("CreateSongList");
                        Messenger.Default.Unregister<object>("CloseWindow");
                        Messenger.Default.Unregister<object>("MiniumWindow");
                        Messenger.Default.Unregister<object>("MaxiumWindow");

                        string strSongList = JsonConvert.SerializeObject(songLists);
                        string configPath = System.IO.Path.Combine(System.Environment.CurrentDirectory, (string)Properties.Settings.Default["DefaultSongListConfig"]);
                        File.WriteAllText(configPath, strSongList);
                    });
                }
                return onClosingCommand;
            }

        }

        private RelayCommand minCommand;

        public RelayCommand MinCommand
        {
            get
            {
                if (minCommand == null)
                {
                    minCommand = new RelayCommand(()=>
                    {
                        Messenger.Default.Send<object>("Max", "MiniumWindow");
                    });
                }
                return minCommand;
            }
           
        }

        private RelayCommand onCloseCommand;

        public RelayCommand OnCloseCommand
        {
            get
            {
                if (onCloseCommand == null)
                {
                    onCloseCommand = new RelayCommand(() =>
                    {
                        Messenger.Default.Send<object>("Close","CloseWindow");
                    });
                }
                return onCloseCommand;
            }
        }

        private RelayCommand maxiumCommand;

        public RelayCommand MaxiumCommand
        {
            get
            {
                if (maxiumCommand == null)
                {
                    maxiumCommand = new RelayCommand(()=>
                    {
                        Messenger.Default.Send<object>("Max", "MaxiumWindow");
                    });
                }
                return maxiumCommand;
            }
        }

        private RelayCommand openChangeSkinCommand;

        public RelayCommand OpenChangeSkinCommand
        {
            get
            {
                if (openChangeSkinCommand == null)
                {
                    openChangeSkinCommand = new RelayCommand(()=>
                    {
                        skinPopupShow = !skinPopupShow;
                    });
                }
                return openChangeSkinCommand;
            }
        }

        private RelayCommand<string> changeSkinCommand;

        public RelayCommand<string> ChangeSkinCommand
        {
            get
            {
                if (changeSkinCommand == null)
                {
                    changeSkinCommand = new RelayCommand<string>((p)=>
                    {
                        ChangeSkin(p);
                        skinPopupShow = false;
                    });
                }
                return changeSkinCommand;
            }
        }


        private List<Audio> playList;

        public List<Audio> PlayList
        {
            get { return playList; }
            set
            {
                playList = value;
                RaisePropertyChanged(nameof(PlayList));
            }
        }


        public void ChangeSkin(string styleFileName)
        {
            ResourceDictionary resDic = null;
            string res_Path = string.Format("Styles/{0}.xaml",styleFileName);
            resDic = System.Windows.Application.LoadComponent(new Uri(res_Path, UriKind.RelativeOrAbsolute)) as ResourceDictionary;
            if (resDic != null)
            {
                Application.Current.Resources = resDic; 
            }
        }

        public MainViewModel()
        {
            player = _container.GetService<IPlayer>();
            //注册新增歌单消息
            Messenger.Default.Register<SongList>(this,"CreateSongList",new Action<SongList>((p) =>
            {
                SongLists.Add(p);
            }
           ));

            //注册进度条委托
            player.ProgressChanged += (p) =>
            {
                AudioProgress = p;
            };

            //加载默认歌单
            string configPath = System.IO.Path.Combine(System.Environment.CurrentDirectory,(string)Properties.Settings.Default["DefaultSongListConfig"]);
            if(File.Exists(configPath))
            {
                string config = File.ReadAllText(configPath);
                SongLists = JsonConvert.DeserializeObject<ObservableCollection<SongList>>(config);
            }
        }

        
    }
}
